package com.room.user.controller;

import java.util.Arrays;
import java.util.Map;

import com.project.utils.PageUtils;
import com.project.utils.R;
import com.room.user.util.JwtUtil;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.room.user.entity.UserEntity;
import com.room.user.service.UserService;




/**
 * 
 *
 * @author brh
 * @email sunlightcs@gmail.com
 * @date 2022-03-23 16:59:36
 */
@RestController
@RequestMapping("user/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private JwtUtil jwtUtil;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = userService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{userId}")
    public R info(@PathVariable("userId") Integer userId){
		UserEntity user = userService.getById(userId);

        return R.ok().put("user", user);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody UserEntity user){
        //首先判断验证码是否正确!
        //从redis中获取该手机号的验证码
        String s = redisTemplate.opsForValue().get(user.getUserTel());
        if (user.getYzm().equals(s)){//验证码
            user.setUserStatus("1");
            user.setUserMaibox("新人");
            userService.save(user);
            return R.ok();
        }else{
            return R.error("验证码错误!");
        }
    }

    /**
     *修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody UserEntity user){
		userService.updateById(user);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] userIds){
		userService.removeByIds(Arrays.asList(userIds));

        return R.ok();
    }

    //登录
    @RequestMapping("/login")
    public R login(@RequestBody UserEntity userEntity){
        //首先判断用户名是否存在
        String userNikeName = userEntity.getUserNikeName();
        UserEntity user = userService.findByName(userNikeName);
        if (user!=null){//该用户存在
            //判断密码是否正确
            if (user.getUserPwd().equals(userEntity.getUserPwd())){//正确
                String token = jwtUtil.createJWT(user.getUserId() + "", user.getUserName(), "user");
                return R.ok().put("user",user).put("token",token);
            }else{//密码不正确
                return R.error("密码错误!");
            }
        }else{//不存在
            return R.error("该用户不存在!");
        }
    }

    //发送短信验证码
    @RequestMapping("/sendYzm")
    public R sendYzm(@RequestParam String phone){
        //查询手机号是否已经被注册
        UserEntity user = userService.findByPhone(phone);
        if (user!=null){
            return R.error("手机号已经被注册");
        }else{
            //使用消息中间件
            rabbitTemplate.convertAndSend("amp.RoomUser","seng.Yzm",phone);
            return R.ok();
        }
    }

}
